package com.zhou.framework.elasticsearch;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.rest.RestStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author lang.zhou
 * @date 2022/5/16 16:27
 */
@Slf4j
@Component
public class ElasticsearchService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    public void createIndex(CreateIndexRequest request){
        try {
            IndicesClient index = restHighLevelClient.indices();
            CreateIndexResponse response = index.create(request, RequestOptions.DEFAULT);
            log.info(response.index());
        }catch (Exception e){
            log.error("创建索引异常",e);
        }
    }
    public void deleteIndex(DeleteIndexRequest request){
        try {
            AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
            log.info(response.toString());
        }catch (Exception e){
            log.error("删除索引异常",e);
        }
    }
    public SearchResponse search(SearchRequest search){
        SearchResponse response = null;
        try {
            //是否忽略不可用的索引
            //是否允许索引不存在
            //通配符表达式将扩展为打开的索引
            //通配符表达式将扩展为关闭的索引
            IndicesOptions opt = IndicesOptions.fromOptions(true, true, true, false);
            search.indicesOptions(opt);
            response = restHighLevelClient.search(search, RequestOptions.DEFAULT);
            if(response.status()!= RestStatus.OK){
                return null;
            }
        }catch (Exception e){
            log.error("搜索发生异常：",e);
        }
        return  response;
    }
    public GetResponse get(GetRequest getRequest){
        GetResponse getResponse = null;
        try {
            getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
            if(!getResponse.isExists()){
                return  null;
            }
        }catch (IOException ex){
            log.error("get查询异常："+ex.getMessage());
        }
        return  getResponse;
    }
    public void update(UpdateRequest request){
        try {
            request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
            UpdateResponse response = restHighLevelClient.update(request,RequestOptions.DEFAULT);
            if(response.status()!= RestStatus.OK){
                log.error("更新异常："+response.toString());
            }
        } catch (IOException e) {
            log.error("更新异常："+e.getMessage());
        }
    }
    public void insert(IndexRequest request){
        try {
            IndexResponse response = restHighLevelClient.index(request,RequestOptions.DEFAULT);
            if(response.getShardInfo().status()!= RestStatus.OK){
                log.error("新增异常："+response.toString());
            }
        } catch (IOException e) {
            log.error("新增异常",e);
        }
    }
    public void delete(DeleteRequest deleteRequest){
        try {
            deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
            DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
            if(deleteResponse.status()!=RestStatus.OK){
                log.error("删除数据异常："+deleteResponse.toString());
            }
        }catch (IOException e){
            log.error("删除数据异常",e);
        }
    }
}
